/**
 * 头部js
 */
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
	document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F47e4547191e45a7c8fadc1a4497987d4' type='text/javascript'%3E%3C/script%3E"));
	
$(function(){
	var fans=$("#sessionFans").val();
	var weixin=$("#sessionWeixin").val();
	var userName="";
	var type=$("#sessionUser").val();
	if(type==2){	
		userName=$("#sessionUserName").val();
	}
	var header="";
	header+='<div class="state">';
	header+='<ul class="state-ul">';
	header+='	<li class="state-title" >水泥联赛官方微博</li>';
	header+='   <li class="state-content"><a href="http://weibo.com/shuiniliansai">点击此处</a></li>';
	header+='   <li class="state-title">水泥联赛官方微信号</li>';
	header+='	<li class="state-content"><a style="color:#00008B;">shuiniliansai</a></li>';
	if(userName!=""){
		header+='<li class="state-title"><a target="_blank" href="http://bbs.cu-league.com">'+userName+'</a><span onclick="openWin(12)">  退出</span></li>';
	}
	header+='</ul>';
	if(userName==""){
		header+='<button class="signbtn" id="signbtn">登录</button>		';
	}	
	header+='<!--下面是登陆框-->';
	header+='<div class="signframe" id="signframe">';
	header+='	<div class="sf-sign" id="sf-sign">';
	header+='		<div class="sf-title">登录ShuiNi</div>';
	header+='		<div class="sf-inputline">';
	header+='			<label class="sf-label">账号：</label>';
	header+='			<input class="sf-input" type="text" id="userName" name="userName" value="请输入账号"/>';
	header+='		</div>';
	header+='		<div class="sf-inputline">';
	header+='			<label class="sf-label">密码：</label>';
	header+='		<input class="sf-input" type="password" id="password" name="password"/>';
	header+='	</div>';
	header+='	<div class="sf-line"><button class="sf-btn" id="si-btn" onclick="chklogon()">登录</button></div>';
	header+='	<div class="sf-line">还没有账号么？<a href="#" id="sf-a">点击此处</a>创建ShuiNi账号</div>';
	header+='	<div class="sf-close">关闭</div>';
	header+='</div>';
	header+='<div class="sf-regist" id="sf-regist">';
	header+='	<div class="sf-title">登记ShuiNi账号</div>';
	header+='	<div class="sf-inputline">';
	header+='	<label class="sf-label">账号：</label>';
	header+='	<input class="sf-input" type="text" id="userName1" name="userName" value="请输入账号"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">密码：</label>';
	header+='	<input class="sf-input" type="password" id="password1" name="password"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">确认密码：</label>';
	header+='	<input class="sf-input" type="password" id="passWordAgain1" name="passWordAgain"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">邮箱：</label>';
	header+='	<input class="sf-input" type="text" id="email" name="email"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">微博 ：</label>';
	header+='	<input class="sf-input" type="text" id="microblog" name="microblog"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">人人：</label>';
	header+='	<input class="sf-input" type="text" id="renren" name="renren"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">手机：</label>';
	header+='	<input class="sf-input" type="text" id="phone" name="phone" maxlength="11"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">微信：</label>';
	header+='	<input class="sf-input" type="text" id="wechat" name="wechat"/>';
	header+='</div>';
	header+='<div class="sf-inputline">';
	header+='	<label class="sf-label">邀请码：</label>';
	header+='	<input class="sf-input" type="text" id="invitationCode" name="invitationCode" value="球队邀请码如无可不填"/>';
	header+='</div>';
	header+='<div class="sf-line"><button class="sf-btn" id="re-btn" onclick="chkRegister()">注册并登录</button></div>';
	header+='			<div class="sf-close">关闭</div>';
	header+='		</div>';
	header+='	</div>';
	header+='</div>';
	header+='<div class="navigation">';
	header+='	<ul class="nav-ul">';
	header+='	    <li class="nav-home nav-on" onclick="openWin(1)"></li>';
	header+='		<li class="nav-match" id="nav-match" onclick="openWin(2)">赛程</li>';
	header+='		<li class="nav-team" id="nav-team" onclick="openWin(3)">球队</li>';
	header+='		<li class="nav-rank" id="nav-rank" onclick="openWin(4)">排名</li>';
	header+='		<li class="nav-data" id="nav-data" onclick="openWin(5)">数据</li>';
	header+='		<li class="nav-score" id="nav-score" onclick="openWin(6)">比分</li>';
	header+='		<li class="nav-news" id="nav-news" onclick="openWin(7)">新闻</li>';
	header+='		<li class="nav-picture" id="nav-picture" onclick="openWin(9)">图片</li>';
	header+='		<li class="nav-sns"  id="nav-sns" onclick="openWin(11)">社区</li>';
	header+='	</ul>';
	header+='	<div class="searchblock">';
	header+='		<label class="search-label"></label>';
	header+='		<input class="search-input" />';
	header+='	</div>';
	header+='</div>';
	header+='<a href="/index.html"><div class="sectionlogo">';
	header+='</div></a>';
	$("#header").html(header);
	
	var fenxiang="";
	fenxiang+='<a href="#" class="bds_more" data-cmd="more"></a>';
	fenxiang+='<a title="分享到微信" href="#" class="bds_weixin" data-cmd="weixin"></a>';
	fenxiang+='<a title="分享到新浪微博" href="#" class="bds_tsina" data-cmd="tsina"></a>';
	fenxiang+='<a title="分享到QQ空间" href="#" class="bds_qzone" data-cmd="qzone"></a>';
	fenxiang+='<a title="分享到腾讯微博" href="#" class="bds_tqq" data-cmd="tqq"></a>';
	fenxiang+='<a title="分享到人人网" href="#" class="bds_renren" data-cmd="renren"></a>';
	$("#fenxiang").html(fenxiang);
	
	$('.sf-close').bind('click',function(){
		$('#signframe').removeClass("signframesp");
		$('#signframe').hide();
	});
	$('#signbtn').bind('click',function(){
		$('#signframe').find('#sf-sign').show().parent().find('#sf-regist').hide().parent().toggle();
	});
	$('#sf-a').bind('click',function(){
		$('#sf-sign').hide();$('#sf-regist').show();
	});
	$('#userName').focus(function(){
        if($(this).val()=='请输入账号'){
            $(this).val('');
        }       
    });
    $('#userName').blur(function(){
        if($(this).val()==''){
            $(this).val('请输入账号');
        }
    });
    $('#userName1').focus(function(){
        if($(this).val()=='请输入账号'){
            $(this).val('');
        }       
    });
    $('#userName1').blur(function(){
        if($(this).val()==''){
            $(this).val('请输入账号');
        }
    });
    $('#email').focus(function(){
        if($(this).val()=='请输入邮箱'){
            $(this).val('');
        }       
    });
    $('#email').blur(function(){
        if($(this).val()==''){
            $(this).val('请输入邮箱');
        }
    });
    $('#invitationCode').focus(function(){
        if($(this).val()=='球队邀请码如无可不填'){
            $(this).val('');
        }       
    });
    $('#invitationCode').blur(function(){
        if($(this).val()==''){
            $(this).val('球队邀请码如无可不填');
        }
    });
});
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"http://112.124.25.199:8182/images","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
//登陆
function chklogon(){       
	var userName=$("#userName").val();
	var password=$("#password").val();
	if(userName==""){
		alert("登录名不能为空");
		return false;
	}
	if(password==""){
		alert("密码不能为空");
		return false;
	}    	
    $.ajax({
        url:'/userLogin.yson',
        type:'POST',
        data:{userName:userName,password:password},
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        dataType:"json",
        success:function(data) {
        	 if (data.suc) {
//                 alert(data.msg);
//                 window.location.href='/bbs/login.html?userName='+userName+"&password="+password+"&href="+window.location.href;
        		 loginAnswer(userName, password);
             }else{
                 alert(data.msg);
             }
        }
    });

}


//注册
function chkRegister(){       
	var userName=$("#userName1").val();
	var password=$("#password1").val();
	var passWordAgain=$("#passWordAgain1").val();
	var email=$("#email").val();
	var microblog=$("#microblog").val();
	var renren=$("#renren").val();
	var phone=$("#phone").val();
	var wechat=$("#wechat").val();
	var invitationCode=$("#invitationCode").val();
	if(userName==""){
		alert("注册名不能为空");
		return false;
	}
	if(userName.length > 16 || userName.length < 6){        
        alert("请输入6到16位注册名");
        return false;
    }
	if(password==""){
		alert("密码不能为空");
		return false;
	}
	if(password.length > 18 || password.length < 6){        
        alert("请输入6到18位密码");
        return false;
    }
	if(passWordAgain==""){
		alert("确认密码不能为空");
		return false;
	}
	if(password!=passWordAgain){
		alert("两次密码不一致");
		return false;
	}
	if(email==""|| email=="请输入邮箱"){
		alert("邮箱不能为空");
		return false;
	}else{
		var reg = RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);   
        if(reg.test(email)){

        }else{
        	alert("邮箱格式有误");
        	return false;
        }
	}
	if(invitationCode=="球队邀请码如无可不填"){
		invitationCode="";
	}
    $.ajax({
        url:'/userRegister.yson',
        type:'POST',
        data:{userName:userName,password:password,invitationCode:invitationCode,email:email,microblog:microblog,renren:renren,phone:phone,wechat:wechat},
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        dataType:"json",
        success:function(data) {
        	 if (data.suc) {
//                 alert(data.msg);
//                 window.location.href='/bbs/login.html?userName='+userName+"&password="+password+"&href="+window.location.href;
                 loginAnswer(userName, password);
             }else{
                 alert(data.msg);
             }
        }
    });
}
function openWin(type){
	if(type==1){
		window.location.href='/index.html';
	}else if(type==2){
		window.location.href='/match/show.html';
	}else if(type==3){
		window.location.href='/team/teamWebList.html';
	}else if(type==4){
		window.location.href='/ranking/personalRankCampusList.html';
	}else if(type==5){
		window.location.href='/teamDate/teamDataList.html';
	}else if(type==6){
		window.location.href='/frontScore/show.html';
	}else if(type==7){
		window.location.href='/tnews/newsWebList.html';
	
	}else if(type==9){
		window.location.href='/imgModel/pictureList.html';
	
	}else if(type==11){
		window.open('http://bbs.cu-league.com/forum.php');
	}else if(type==12){
		window.location.href='/exit.html';
	}
}

function loginAnswer(userName,password){
	var activityIds = "";
	var questionIds = "";
	var questions = "";
	var questionOrders = "";
	var answers = "";
	var num = 1;
	while(true){
		var activityId = getCookie("activityId_"+num);
		if(activityId == null || activityId == ""){
			break;
		}
		activityIds += activityId + "&&";
		questionIds += getCookie("questionId_"+num) + "&&";
		questions += getCookie("question_"+num) + "&&";
		questionOrders += getCookie("questionOrder_"+num) + "&&";
		answers += getCookie("answer_"+num) + "&&";
		num++;
	}
	if(num>1){
		activityIds = activityIds.substring(0, activityIds.length-2);
		questionIds = questionIds.substring(0, questionIds.length-2);
		questions = questions.substring(0, questions.length-2);
		questionOrders = questionOrders.substring(0, questionOrders.length-2);
		answers = answers.substring(0, answers.length-2);
	}
	if(activityIds !=null && activityIds != "" && questionIds !="" && questionIds != null){
		$.post("/activityFore/checkAnswer.yson", {
			"activityIds" : activityIds,
			"questionIds" : questionIds,
			"questions" : questions,
			"questionOrders" : questionOrders,
			"answers" : answers
		}, function(data) {
			if (data.suc) {
				for(var i=1; i<num; i++){
					delCookie("activityId_"+i);
					delCookie("questionId_"+i);
					delCookie("question_"+i);
					delCookie("questionOrder_"+i);
					delCookie("answer_"+i);
				}
			}
			window.location.href='/bbs/login.html?userName='+userName+"&password="+password+"&href="+window.location.href;
		});	
	} else{
		window.location.href='/bbs/login.html?userName='+userName+"&password="+password+"&href="+window.location.href;
	}
}

//添加cookie 
function addCookie(name,value,expires){ 
	var str=name+"="+escape(value); 
	if(expires!=""){ 
		var date=new Date(); 
		date.setTime(date.getTime()+expires*1000);//expires单位为秒
		str+=";expires="+date.toGMTString(); 
	}
	return str;
} 
	
function getCookie(objName){//获取指定名称的cookie的值 
	var arrStr = document.cookie.split("; "); 
	for(var i = 0;i < arrStr.length;i ++){ 
		var temp = arrStr[i].split("="); 
		if(temp[0] == objName) 
		return unescape(temp[1]); 
	}
}

function delCookie(name){//为了删除指定名称的cookie，可以将其过期时间设定为一个过去的时间 
	var date = new Date(); 
	date.setTime(date.getTime() - 10000); 
	document.cookie = name + "=a; expires=" + date.toGMTString(); 
}
